import axios from 'axios';
import { config } from '@/../config/axios_config.js'; //导入配置文件


axios.defaults.timeout = 1000 * 10; //设置延时，延时到了抛出error让axios走catch线
axios.defaults.baseURL = config.host;
// console.log(axios.defaults.baseURL)
//设置baseURL，让后面传url参数时可以使用相对url，
//如果使用绝对url则优先使用绝对url
axios.defaults.headers.post['Content-Type'] = 'Content-Type:application/x-www-form-urlencoded; charset=UTF-8';//预防跨域

import { Message, Loading } from 'element-ui';
let loading;
function openLoading() {
  loading = Loading.service({
    lock: true,
    background: 'rgba(255,255,255,0.6)',
  });
}
function closeLoading() {
  this.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
    loading.close();
  });
}

let histroy = [];
//请求（发）拦截器，也就是loading.open()的时机
// axios.interceptors.request.use(req => {
//   openLoading()
//   return req
// }, err => {
//   closeLoading()
//   return Promise.reject(err)
// })
//响应（收）拦截器，也就是loading.close()的时机
axios.interceptors.response.use(res => {
  // closeLoading();
  if (res.status === 200) {
    return Promise.resolve(res.data)
  }
  return res
}, err => {
  // closeLoading();
  let ObjErr = JSON.parse(JSON.stringify(err)).config;
  if (err && err.response) {
    switch (err.response.status) {
      case 400: err.message = '请求错误(400)'; break;
      case 401: this.$router.push('/login'); break;
      case 403: err.message = '拒绝访问(403)'; break;
      case 404: err.message = '请求出错(404)'; break;
      case 408: err.message = '请求超时(408)'; break;
      case 500: err.message = '服务器错误(500)'; break;
      case 501: err.message = '服务未实现(501)'; break;
      case 502: err.message = '网络错误(502)'; break;
      case 503: err.message = '服务不可用(503)'; break;
      case 504: err.message = '网络超时(504)'; break;
      case 505: err.message = 'HTTP版本不受支持(505)'; break;
      default: err.message = `连接出错(${err.response.status})!`;
    }
  }
  else {
    err.message = `接口${ObjErr.url}异常`;
  }
  histroy.push(Object.assign(ObjErr, {
    time: new Date()
  }))
  Message.error(`${err.message},请联系技术人员解决`);
  return Promise.reject(err)
})

import { testGet, testPost, testMirrorForce } from './test.js';


let api = {
  testGet, testPost, testMirrorForce,

  getInitConfigs: function ({ ceid }) {
    let p1 = axios.get(`${config.appPort}/api/AppChat/GetInitConfigs`, {
      params: { companyEmployeeId: ceid }
    })
    return Promise.all([p1])
  },//获取初始化信息

  get_App_Cus: function (data) {
    return axios.post(`/api/ChatMsg/GetCustomers`, {
      postParams: {
        imCfg: {
          "eSCompanyId": "",
          "agentid": 0,
          "departmentId": 0,
          "readingType": 0,
          "replyType": 0,
          "isStar": true,
          "isNoReply": true,
          "searchName": "",
          "selftag": "",
          "unSelftag": ""
        },
        crmCfg: {
          "joinTimeBegin": "",
          "joinTimeEnd": "",
          "fromSources": "",
          "tradingMonthBegin": "",
          "tradingMonthEnd": "",
          "earlyWarningLevel": [],
          "ageBegin": 0,
          "ageEnd": 0,
          "isOverdue": true,
          "product": {
            "searchType": 0,
            "searchValues": {}
          },
          "csTag": {
            "searchType": 0,
            "searchValues": {}
          },
          "csBelong": {
            "searchType": 0,
            "searchValues": {}
          },
          "character": {
            "searchType": 0,
            "searchValues": {}
          },
          "servicePreferences": {
            "searchType": 0,
            "searchValues": {}
          },
          "operationalPreference": {
            "searchType": 0,
            "searchValues": {}
          }
        }
      }
    }, {
      params: data
    })
  },// 1.1获取工作台和1.1获取客户列表 合并为2.1获取客户列表\工作台

};

export { axios, api, histroy };